#include "pageoperation.h"
#include "ui_pageoperation.h"
#include <QDebug>

PageOperation::PageOperation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageOperation)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pTrain = new Train(this);
    m_pTrain->setGeometry(0,0,1224,390);
    connect(m_pTrain,SIGNAL(GoToFirePage()),this,SLOT(slot_gotoFirepage()));
    connect(m_pTrain,SIGNAL(GoToPECUPage()),this,SLOT(slot_gotoPECUpage()));

    atc_mainwindow_ = new SignalMainpage(this);
    atc_mainwindow_->move(0,355);


#ifndef RUN_IN_BOARD
    ui->FaultBar->setText("2023-2-23 15:59:58 车号:1  等级:1    转向架1冷却热状态或电源接触器反馈状态异常");
#endif
    setPisMannualBtnsActive(false);
}

PageOperation::~PageOperation()
{
    delete ui;
}

void PageOperation::slot_gotoFirepage()
{
    signal_show_operation_page(Page_Vehcile_Fire);
}

void PageOperation::slot_gotoPECUpage()
{
    signal_show_operation_page(Page_Vehcile_PECU);
}

void PageOperation::on_Btn_SpdLmtHint_clicked()
{
    signal_show_operation_page(Page_Operation_SpdLimit);
}

void PageOperation::on_Btn_EBTrig_clicked()
{
    signal_show_operation_page(Page_Operation_EBTrig);
}

void PageOperation::on_Btn_Bypass_clicked()
{
    signal_show_operation_page(Page_Operation_Bypass);
}

void PageOperation::on_Btn_SelfTest_clicked()
{
    signal_show_operation_page(Page_Operation_SelfTest);
}

void PageOperation::on_Btn_TracBlock_clicked()
{
    signal_show_operation_page(Page_Operation_TracBlock);
}

Train* PageOperation::train() const
{
    return m_pTrain;
}

SignalMainpage* PageOperation::atc_page() const
{
    return atc_mainwindow_;
}

void PageOperation::slotBypassBtnStat(bool xAct)
{
    if(xAct)
        ui->Btn_Bypass->setStyleSheet("QPushButton{background-color:rgb(255,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                      "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
    else
        ui->Btn_Bypass->setStyleSheet("QPushButton{background-color:rgb(0,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                          "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
}

void PageOperation::slotSpeedLimitBtnStat(bool xAct)
{
    if(xAct)
        ui->Btn_SpdLmtHint->setStyleSheet("QPushButton{background-color:rgb(255,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                      "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
    else
        ui->Btn_SpdLmtHint->setStyleSheet("QPushButton{background-color:rgb(0,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                          "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
}

void PageOperation::slotTracBlockBtnStat(bool xAct)
{
    if(xAct)
        ui->Btn_TracBlock->setStyleSheet("QPushButton{background-color:rgb(255,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                      "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
    else
        ui->Btn_TracBlock->setStyleSheet("QPushButton{background-color:rgb(0,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                          "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
}

void PageOperation::slotEBTrigBtnStat(bool xAct)
{
    if(xAct)
        ui->Btn_EBTrig->setStyleSheet("QPushButton{background-color:rgb(255,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                      "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
    else
        ui->Btn_EBTrig->setStyleSheet("QPushButton{background-color:rgb(0,0,0);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}"
                                          "QPushButton:pressed{background-color:rgb(0,100,255);font:16pt 'Arial';color:rgb(255,255,255);border:2px solid rgb(64,64,64);border-radius:8px;}");
}

void PageOperation::activateAlarm(uint id, uint time, QString car, QString description,QString level)
{
    AlarmInfo info;
    info.id = id;
    info.time = QDateTime::fromTime_t(time).toString("yyyy-MM-dd hh:mm:ss");
    info.car = car;
    info.description = description;
    info.level = level;
    alarmlist.append(info);
    QString fault_description = info.time + "  " +
                                QString("车号:%1").arg(car) + "  " +
                                QString("等级:%1").arg(level) + "    " +
                                info.description ;
    ui->FaultBar->setText(fault_description);
    if(level == "1" || level == "2")
        ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:red;");
    else if(level == "3")
        ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:yellow;");
    else
        ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:rgb(64,64,64);");
}

void PageOperation::on_AckBtn_clicked()
{
    if(alarmlist.isEmpty())
        return;
    else if(alarmlist.size() == 1)
    {
        alarmlist.clear();
        ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:rgb(64,64,64);");
        ui->FaultBar->setText("");
    }
    else
    {
        alarmlist.removeLast();
        if(alarmlist.isEmpty())
            return;
        QString level = alarmlist.last().level;
        QString fault_description = alarmlist.last().time + "  " +
                                    QString("车号:%1").arg(alarmlist.last().car) + "  " +
                                    QString("等级:%1").arg(level) + "    " +
                                    alarmlist.last().description ;
        ui->FaultBar->setText(fault_description);
        if(level == "1" || level == "2")
            ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:red;");
        else if(level == "3")
            ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:yellow;");
        else
            ui->FaultBar->setStyleSheet("color:white;font:16pt 'Arial';background-color:rgb(64,64,64);");
    }
}

void PageOperation::on_BtnDepartPA_clicked()
{
    emit signal_DepartPA(true);
    ui->BtnDepartPA->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_DepartPA(false);
        ui->BtnDepartPA->setEnabled(true);
    });
}

void PageOperation::on_BtnArrivedPA_clicked()
{
    emit signal_ArrivedPA(true);
    ui->BtnArrivedPA->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_ArrivedPA(false);
        ui->BtnArrivedPA->setEnabled(true);
    });
}

void PageOperation::setPisMannualBtnsActive(bool xActive)
{
    ui->BtnDepartPA->setEnabled(xActive);
    ui->BtnArrivedPA->setEnabled(xActive);
    ui->Btn_NextStation->setEnabled(xActive);
    ui->Btn_PreStation->setEnabled(xActive);
}

void PageOperation::on_Btn_NextStation_clicked()
{
    emit signal_NextStationPA();
}

void PageOperation::on_Btn_PreStation_clicked()
{
    emit signal_PreStationPA();
}

void PageOperation::setATCComflt(bool xComflt)
{
    atc_mainwindow_->Set_ATC_ComFlt_Page(xComflt);
}
